package mspbots.next.ticket.core.data.rules;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Jay Yang
 */
@RestController
@RequestMapping("rules/{tenantId}")
@RequiredArgsConstructor
public class RulesController {

    private final RulesServiceImpl rulesService;

    @GetMapping
    public Flux<Rule> findByTenant(@PathVariable Long tenantId) {
        return this.rulesService.findByTenant(tenantId);
    }

    @PostMapping
    public Mono<Rule> saveOne(@PathVariable Long tenantId,
                              @Valid @RequestBody RuleRequest request) {
        return this.rulesService.saveOne(tenantId, request);
    }

    @PostMapping("batch")
    public Flux<Rule> batchSave(@PathVariable Long tenantId,
                                @Valid @RequestBody List<RuleRequest> requests) {
        return this.rulesService.batchSave(tenantId, requests);
    }

}
